import type { DemoDatasource } from '@/demo/types'
import { requireModule } from '@/utils/mpHelper'

export async function loadFileModule<TModule>(path: string): Promise<TModule> {
  const mod = await requireModule<Record<string, { default: TModule }>>(path)
  const fileProp = Object.keys(mod).find((key) => key.includes('File'))
  if (!fileProp) {
    throw new Error('File Property does not exist')
  }
  const fileMod = mod[fileProp]
  return fileMod as TModule
}

export async function loadDatasource(path: string) {
  const fileMod = await loadFileModule<{ default: DemoDatasource }>(path)
  const datasource = fileMod.default
  console.log('Datasource => ', datasource)
  return datasource
}
